package cn.yangtianxia.basic.controller;

import cn.yangtianxia.basic.dto.RegisterDto;
import cn.yangtianxia.basic.exception.BusinessException;
import cn.yangtianxia.basic.service.IVerifyCodeService;
import cn.yangtianxia.basic.util.AjaxResult;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

@RestController
@RequestMapping("/verifyCode")
public class VerifyCodeController {

    @Autowired
    private IVerifyCodeService verifyCodeService;

    @GetMapping("/image/{key}")
    public AjaxResult imgCode(@PathVariable("key") String key){
        try {
            // service返回的是Base64加密的String
            String base64Str = verifyCodeService.graph(key);
            return AjaxResult.me().setResultObj(base64Str);
        } catch (BusinessException e) {// 自定义业务异常
            e.printStackTrace();
            return AjaxResult.me().setSuccess(false).setMessage(e.getMessage());
        } catch (Exception e) {
            e.printStackTrace();
            return AjaxResult.me().setSuccess(false).setMessage("系统繁忙，稍后重试！！！");
        }
    }

    @PostMapping("/smsCode")
    public AjaxResult smsCode(@RequestBody RegisterDto dto){
        try {
            verifyCodeService.smsCode(dto);
            return AjaxResult.me();
        } catch (BusinessException e) {// 自定义业务异常
            e.printStackTrace();
            return AjaxResult.me().setSuccess(false).setMessage(e.getMessage());
        } catch (Exception e) {
            e.printStackTrace();
            return AjaxResult.me().setSuccess(false).setMessage("系统繁忙，稍后重试！！！");
        }
    }

    @PostMapping("/smCode")
    public AjaxResult smCode(@RequestBody RegisterDto dto){
        try {
            verifyCodeService.smCode(dto);
            return AjaxResult.me();
        } catch (BusinessException e) {// 自定义业务异常
            e.printStackTrace();
            return AjaxResult.me().setSuccess(false).setMessage(e.getMessage());
        } catch (Exception e) {
            e.printStackTrace();
            return AjaxResult.me().setSuccess(false).setMessage("系统繁忙，稍后重试！！！");
        }
    }
}
